/*
**  Apache License, Version 2.0
**  Notes:Use this command to anonymously check out the latest project source code:
**  Non-members may check out a read-only working copy anonymously over HTTP.
**  SVN checkout http://YHHCatiaBaseUtlWsp.googlecode.com/svn/trunk/ 
**
**  Author:		Kevin Yang
**  Email:		Bigearrabbit@qq.com
**
**  DATE:		2013-4-4
**
**  File Name:	YHHSpecAttrAccess.cpp
**
**  Notes:
** 
*/
// Local Framework
#include "YHHSpecAttrAccess.h"
#include "YHHCatiaBaseDefine.h"

// ObjectSpecsModeler
#include "CATIDescendants.h"
#include "CATISpecAttrAccess.h"

// MecModInterfaces Framework
#include "CATIMfBRep.h"

CATImplementClass ( YHHSpecAttrAccess ,
				   DataExtension       ,    
				   CATBaseUnknown      ,
				   CATNull        );

//-----------------------------------------------------------------------------
YHHSpecAttrAccess::YHHSpecAttrAccess()
:CATBaseUnknown()
,_piSpecAttrAccessOnCC(NULL)
{

}
//-----------------------------------------------------------------------------
YHHSpecAttrAccess::~YHHSpecAttrAccess()
{
	_piSpecAttrAccessOnCC = NULL;//not release
}
//-----------------------------------------------------------------------------
CATISpecAttrKey * YHHSpecAttrAccess::GetAttrKey(const char* iName)const
{
	//input check
	if(NULL == _piSpecAttrAccessOnCC) return NULL;//input error
	if(NULL == iName) return NULL;//input error
	if (*iName == 0)  return NULL;//input error
	HRESULT rc = E_FAIL;
	return _piSpecAttrAccessOnCC->GetAttrKey(iName);
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetListValue( const char* iName
										, CATListValCATISpecObject_var & oList ) const
{
	oList.RemoveAll();//clear
	CATListValCATBaseUnknown_var * pList = NULL;//set NULL
	HRESULT rc = this->GetListValue(iName,&pList);//get list
	if (FAILED(rc))return rc;//error

	//copy list
	for (int i=1;i<=pList->Size();i++)
	{
		oList.Append((*pList)[i]);//append
	}//for i
	return S_OK;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetListValue( const char* iName
										, CATListValCATBaseUnknown_var ** oppList ) const
{
	if(!oppList) return E_INVALIDARG;//error
	*oppList = NULL;//set NUL
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error
	*oppList = _piSpecAttrAccessOnCC->ListSpecObjects(piSpecAttrKeyOnSpec);//get list
	return *oppList == NULL ? E_FAIL:S_OK;//return
}
//-----------------------------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetSpecValue(const char* iName,CATISpecObject_var & oValue) const
{
	oValue = NULL_var;//set null
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	oValue = _piSpecAttrAccessOnCC->GetSpecObject(piSpecAttrKeyOnSpec);// Gets the Value
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return S_OK ;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetValue(const char* iName,CATUnicodeString & oValue) const
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	oValue = _piSpecAttrAccessOnCC->GetString(piSpecAttrKeyOnSpec);//Get the Value
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return S_OK ;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetValue(const char* iName,double & oValue) const
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	oValue = _piSpecAttrAccessOnCC->GetDouble(piSpecAttrKeyOnSpec);//Get the Value
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return S_OK ;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::GetValue(const char* iName,int & oValue) const
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	oValue = _piSpecAttrAccessOnCC->GetInteger(piSpecAttrKeyOnSpec);//Get the Value
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return S_OK ;//ok
}
//-----------------------------------------------------------------------------
void YHHSpecAttrAccess::SetCATISpecAttrAccessPtr(CATISpecAttrAccess * ipValue)
{
	_piSpecAttrAccessOnCC = ipValue;//copy
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetSpecValue(const char* iName,const CATISpecObject_var & iValue)
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	HRESULT rc = S_OK;//HRESULT value
	if ( NULL_var != iValue )//check input
	{
		// If the feature is a Feature BRep it must be aggregated by the CC
		CATIMfBRep *pIMfBRep = NULL ;
		rc = iValue->QueryInterface(IID_CATIMfBRep, (void**) & pIMfBRep);
		if ( SUCCEEDED(rc) )//if is a BRep
		{
			CATISpecObject * pFather = iValue->GetFather() ;
			if ( NULL == pFather )
			{
				CATIDescendants * pIDescendantsOnCC = NULL ;
				rc = QueryInterface( IID_CATIDescendants , (void**) &pIDescendantsOnCC );

				if ( SUCCEEDED(rc) )
				{
					pIDescendantsOnCC->Append(iValue) ;
					YHHRelease(pIDescendantsOnCC);//release
				}
			}else 
			{
				YHHRelease(pFather);//release
				rc = E_FAIL ;
			}

			YHHRelease(pIMfBRep);//release
		}else rc = S_OK ; // it's not an error 

		// Sets the input Spec as input of the CC
		if ( SUCCEEDED(rc) )
		{
			_piSpecAttrAccessOnCC->SetSpecObject(piSpecAttrKeyOnSpec,iValue);
		}

	}else
	{
		// The input input Spec pointer is null 
		_piSpecAttrAccessOnCC->UnsetAttributeValue (piSpecAttrKeyOnSpec);
	}
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return S_OK ;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetValue(const char* iName,const CATUnicodeString & iValue)
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	CATUnicodeString value = _piSpecAttrAccessOnCC->GetString(piSpecAttrKeyOnSpec);//Get AttrKey Value
	if (value == iValue) return S_OK;

	// Sets the input value as input of the CC
	HRESULT rc = _piSpecAttrAccessOnCC->SetString(piSpecAttrKeyOnSpec,iValue);
	YHHRelease(piSpecAttrKeyOnSpec);//release
	return rc ;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetValue(const char* iName,const double & iValue)
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if (NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	double value = _piSpecAttrAccessOnCC->GetDouble(piSpecAttrKeyOnSpec);//Get AttrKey Value
	if (value == iValue) return S_OK;

	// Sets the input value as input of the CC
	HRESULT rc = _piSpecAttrAccessOnCC->SetDouble(piSpecAttrKeyOnSpec, iValue);
	YHHRelease(piSpecAttrKeyOnSpec);
	
	return S_OK ;//ok

}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetListValue( const char* iName
										,const CATListValCATISpecObject_var & iList )
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	_piSpecAttrAccessOnCC->UnsetAttributeValue(piSpecAttrKeyOnSpec);//unset

	//loop to add
	for (int i=0;i<=iList.Size();i++)
	{
		_piSpecAttrAccessOnCC->SetSpecObject(piSpecAttrKeyOnSpec,iList[i]);//add
	}//for i
	YHHRelease(piSpecAttrKeyOnSpec);
	return S_OK;//ok
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetListValue( const char* iName
										,const  CATListValCATBaseUnknown_var & iList )
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if(NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	_piSpecAttrAccessOnCC->UnsetAttributeValue(piSpecAttrKeyOnSpec);//unset

	//loop to add
	for (int i=0;i<=iList.Size();i++)
	{
		_piSpecAttrAccessOnCC->SetSpecObject(piSpecAttrKeyOnSpec,iList[i]);//add
	}//for i
	YHHRelease(piSpecAttrKeyOnSpec);

	return S_OK;//return
}
//---------------------------------------------------------
HRESULT YHHSpecAttrAccess::SetValue(const char* iName,const int & iValue)
{
	CATISpecAttrKey * piSpecAttrKeyOnSpec = this->GetAttrKey(iName);//Get AttrKey pointer
	if (NULL == piSpecAttrKeyOnSpec) return E_INVALIDARG;//input error

	int value = _piSpecAttrAccessOnCC->GetInteger(piSpecAttrKeyOnSpec);//Get AttrKey Value
	if (value == iValue) return S_OK;

	// Sets the input value as input of the CC
	HRESULT rc = _piSpecAttrAccessOnCC->SetInteger(piSpecAttrKeyOnSpec,iValue);
	YHHRelease(piSpecAttrKeyOnSpec);

	return S_OK ;//ok

}
